package org.dromara.system.service.impl;

import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.dromara.system.mapper.YzlScheduleFormationMapper;
import org.dromara.system.mapper.YzlScheduleTaskMapper;
import org.springframework.stereotype.Service;
import org.dromara.system.domain.bo.YzlScheduleProgressBo;
import org.dromara.system.domain.vo.YzlScheduleProgressVo;
import org.dromara.system.domain.YzlScheduleProgress;
import org.dromara.system.mapper.YzlScheduleProgressMapper;
import org.dromara.system.service.IYzlScheduleProgressService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 任务上报Service业务层处理
 *
 * @author Lion Li
 * @date 2025-09-15
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class YzlScheduleProgressServiceImpl implements IYzlScheduleProgressService {

    private final YzlScheduleProgressMapper baseMapper;

    private final YzlScheduleFormationMapper formationMapper;

    private  final YzlScheduleTaskMapper taskMapper;

    /**
     * 查询任务上报
     *
     * @param id 主键
     * @return 任务上报
     */
    @Override
    public YzlScheduleProgressVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询任务上报列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 任务上报分页列表
     */
    @Override
    public TableDataInfo<YzlScheduleProgressVo> queryPageList(YzlScheduleProgressBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<YzlScheduleProgress> lqw = buildQueryWrapper(bo);
        Page<YzlScheduleProgressVo> result = baseMapper.selectVoPageDept(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的任务上报列表
     *
     * @param bo 查询条件
     * @return 任务上报列表
     */
    @Override
    public List<YzlScheduleProgressVo> queryList(YzlScheduleProgressBo bo) {
        LambdaQueryWrapper<YzlScheduleProgress> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoListDept(lqw);
    }

    private LambdaQueryWrapper<YzlScheduleProgress> buildQueryWrapper(YzlScheduleProgressBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<YzlScheduleProgress> lqw = Wrappers.lambdaQuery();
        lqw.orderByAsc(YzlScheduleProgress::getId);
        lqw.eq(bo.getDeptId() != null, YzlScheduleProgress::getDeptId, bo.getDeptId());
        lqw.eq(bo.getUserId() != null, YzlScheduleProgress::getUserId, bo.getUserId());
        lqw.eq(StringUtils.isNotBlank(bo.getRemarks()), YzlScheduleProgress::getRemarks, bo.getRemarks());
        lqw.eq(bo.getTaskId() != null, YzlScheduleProgress::getTaskId, bo.getTaskId());
        lqw.like(StringUtils.isNotBlank(bo.getTaskName()), YzlScheduleProgress::getTaskName, bo.getTaskName());
        lqw.eq(bo.getFormationId() != null, YzlScheduleProgress::getFormationId, bo.getFormationId());
        lqw.eq(bo.getReportUserId() != null, YzlScheduleProgress::getReportUserId, bo.getReportUserId());
        lqw.like(StringUtils.isNotBlank(bo.getReportUserName()), YzlScheduleProgress::getReportUserName, bo.getReportUserName());
        lqw.eq(bo.getStartTime() != null, YzlScheduleProgress::getStartTime, bo.getStartTime());
        lqw.eq(bo.getEndTime() != null, YzlScheduleProgress::getEndTime, bo.getEndTime());
        lqw.eq(bo.getPercentage() != null, YzlScheduleProgress::getPercentage, bo.getPercentage());
        lqw.eq(StringUtils.isNotBlank(bo.getDuration()), YzlScheduleProgress::getDuration, bo.getDuration());
        lqw.eq(StringUtils.isNotBlank(bo.getFilesPath()), YzlScheduleProgress::getFilesPath, bo.getFilesPath());
        return lqw;
    }

    /**
     * 新增任务上报
     *
     * @param bo 任务上报
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(YzlScheduleProgressBo bo) {
        YzlScheduleProgress add = MapstructUtils.convert(bo, YzlScheduleProgress.class);
        validEntityBeforeSave(add);
        Long duration = getChaTime(add);
        add.setDuration(duration+"");
        boolean flag = baseMapper.insert(add) > 0;
        formationMapper.updateFormationCompletionById(bo.getFormationId());
        taskMapper.updateCompletionPercentageById(bo.getPercentage(),bo.getTaskId());
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改任务上报
     *
     * @param bo 任务上报
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(YzlScheduleProgressBo bo) {
        YzlScheduleProgress update = MapstructUtils.convert(bo, YzlScheduleProgress.class);
        validEntityBeforeSave(update);
        Long duration = getChaTime(update);
        update.setDuration(duration+"");
        formationMapper.updateFormationCompletionById(bo.getFormationId());
        taskMapper.updateCompletionPercentageById(bo.getPercentage(),bo.getTaskId());
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(YzlScheduleProgress entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除任务上报信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }

    /**
     * 计算两个时间的工期
     * @param params
     * @return
     */
    public Long getChaTime(YzlScheduleProgress params){
        Long starTime=params.getStartTime().getTime();
        Long endTime=params.getEndTime().getTime();
        Long num=endTime-starTime;//时间戳相差的毫秒数
        return  (num/24/60/60/1000)+1 ;//除以一天的毫秒数
    }


}
